﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace AnimalWars2
{
    public class AnimatedTexture
    {
        private int m_Framecount;
        public Texture2D m_Texture;
        private float m_TimePerFrame;
        private int m_Frame;
        private float m_TotalElapsed;
        private bool m_Paused;
        private bool m_Stopped;
        public SpriteEffects m_SpriteEffects = SpriteEffects.None;
        public Color m_Color = Color.White;

        public float m_Rotation, m_Scale, m_Depth;
        public Vector2 m_Origin;

        public AnimatedTexture(Vector2 origin, float rotation, float scale, float depth)
        {
            this.m_Origin = origin;
            this.m_Rotation = rotation;
            this.m_Scale = scale;
            this.m_Depth = depth;
            m_Stopped = true;
        }

        public void Load(ContentManager content, string asset, int frameCount, int framesPerSec)
        {
            m_Framecount = frameCount;
            m_Texture = content.Load<Texture2D>(asset);
            m_TimePerFrame = (float)1 / framesPerSec;
            m_Frame = 0;
            m_TotalElapsed = 0;
            m_Stopped = true;
            m_Paused = false;
        }

        // class AnimatedTexture
        public void UpdateFrame(float elapsed)
        {
            if (!m_Paused)
            {
                m_TotalElapsed += elapsed;

                if (m_TotalElapsed > m_TimePerFrame)
                {
                    m_Frame++;
                    // Keep the Frame between 0 and the total frames, minus one.
                    m_Frame = m_Frame % m_Framecount;
                    m_TotalElapsed -= m_TimePerFrame;
                }
            }
        }

        public void DrawFrame(SpriteBatch batch, Vector2 position)
        {
            DrawFrame(batch, position, new Vector2(m_Scale,m_Scale));
        }

        public void DrawFrame(SpriteBatch batch, Vector2 position, Vector2 scale)
        {
            if (!m_Stopped)
            {
                int FrameWidth = m_Texture.Width / m_Framecount;
                Rectangle sourcerect = new Rectangle(FrameWidth * m_Frame, 0, FrameWidth, m_Texture.Height);
                batch.Draw(m_Texture, position, sourcerect, m_Color, m_Rotation, m_Origin, scale, m_SpriteEffects, m_Depth);
            }
        }

        public bool IsPaused
        {
            get { return m_Paused; }
        }

        public void Reset()
        {
            m_Frame = 0;
            m_TotalElapsed = 0f;
        }

        public void Stop()
        {
            m_Stopped = true;
            Pause();
            Reset();
        }

        public void Play()
        {
            m_Stopped = false;
            m_Paused = false;
        }

        public void Pause()
        {
            m_Paused = true;
        }
    }
}
